package com.fortune.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.MRoleDao;
import com.fortune.dto.in.mRole.MRoleAddDto;
import com.fortune.dto.in.mRole.MRoleEditDto;
import com.fortune.dto.in.mRole.MRoleListDto;
import com.fortune.entity.MPosition;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.MRoleService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.mRole.MRoleB;
import com.fortune.vo.mRole.MRoleDetail;
import com.fortune.vo.mRole.PermissionInMRole;
@Service
public class MRoleServiceImpl implements MRoleService {
	@Autowired
	private MRoleDao mRoleDao;

	@Override
	public PagingData<MRoleB> listMRoles(MRoleListDto listDto, String mId) {
		List<MRoleB> roles = mRoleDao.listMRoles(listDto, mId);
		int count = mRoleDao.countRoles(listDto, mId);
		PagingData<MRoleB> data = new PagingData<>();
		data.setData(roles);
		data.setRecordsTotal(count);
		return data;
	}

	@Override
	@Transactional
	public int insertRoleWithPermissions(MRoleAddDto mRoleAddDto, String mId) {
		String roleId = UUIDUtil.get32UUID();
		int result = mRoleDao.insertRole(mRoleAddDto,mId,roleId);
		if (result!=1) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		int exsistPermissionCount = mRoleDao.countExsistPermissionCount(mRoleAddDto.getPermissions());
		if (exsistPermissionCount!=mRoleAddDto.getPermissions().size()) {
			throw new InsertInnerErrorException("不能给岗位赋予不存在的权限或重复的权限");
		}
		result = mRoleDao.insertRolePermissions(mRoleAddDto.getPermissions(),mId,roleId);
		if (result!=mRoleAddDto.getPermissions().size()) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		return result;
	}

	@Override
	public MPosition getPositionByMIdAndName(String name, String mId) {
		return mRoleDao.getPositionByMIdAndName(name,mId);
	}

	@Override
	public MPosition getPositionByMIdAndNameAndRoleId(String name, String mId, String roleId) {
		return mRoleDao.getPositionByMIdAndNameAndRoleId(name,mId,roleId);
	}

	@Override
	public MPosition getPositionByMIdAndRoleId(String mId, String roleId) {
		return mRoleDao.getPositionByMIdAndRoleId(mId,roleId);
	}

	@Override
	@Transactional
	public int updateRoleWithPermissions(MRoleEditDto editDto, String mId) {
		int result = mRoleDao.updateRole(editDto,mId);
		if (result!=1) {
			throw new UpdateInnerErrorException("编辑岗位失败");
		}
		int exsistPermissionCount = mRoleDao.countExsistPermissionCount(editDto.getPermissions());
		if (exsistPermissionCount!= editDto.getPermissions().size()) {
			throw new InsertInnerErrorException("不能给岗位赋予不存在的权限或重复的权限");
		}
		
		mRoleDao.deletePermissionsByRoleId(editDto.getRoleId(),mId);
		result = mRoleDao.insertRolePermissions(editDto.getPermissions(),mId,editDto.getRoleId());
		if (result!= editDto.getPermissions().size()) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		return result;
	}

	@Override
	public int deleteRole(String mId, String roleId) {
		return mRoleDao.deleteRole(mId,roleId);
	}

	@Override
	public MRoleDetail getRoleDetail(String mId, String roleId) {
		MPosition mPosition = mRoleDao.getPositionByMIdAndRoleId(mId, roleId);
		if (mPosition==null) {
			return null;
		}
		MRoleDetail mRoleDetail = new MRoleDetail();
		mRoleDetail.setId(mPosition.getId());
		mRoleDetail.setName(mPosition.getName());
		mRoleDetail.setMemo(mPosition.getMemo());
		List<PermissionInMRole> permissions = mRoleDao.listPermissions();
		List<String> permissionIds = mRoleDao.getPermissionIdsByRole(mId, roleId);
		for (PermissionInMRole first : permissions) {
			first.setSelected(checkSelected(permissionIds, first));
			if (first.getSubPermissions()!=null&&first.getSubPermissions().size()>0) {
				for (PermissionInMRole second : first.getSubPermissions()) {
					second.setSelected(checkSelected(permissionIds, second));
					if (second.getSubPermissions()!=null&&second.getSubPermissions().size()>0) {
						for (PermissionInMRole function : second.getSubPermissions()) {
							function.setSelected(checkSelected(permissionIds, function));
						}
					}
				}
			}
		}
		mRoleDetail.setPermissions(permissions);
		return mRoleDetail;
	}

	@Override
	public int queryRoleExist(String mId, String roleId) {
		return mRoleDao.queryRoleExist(mId,roleId);
	}

	private boolean checkSelected(List<String> permissionIds,PermissionInMRole permission) {
		if (permission==null||permission.getId()==null) {
			return false;
		}
		for (String permissionId : permissionIds) {
			if (permission.getId().equals(permissionId)) {
				return true;
			}
		}
		return false;
	}

}
